package com.example.config;


import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * 异常反馈
 */
@ControllerAdvice  //Controller的增强
public class ErrorAdvice {


    @ExceptionHandler(Exception.class)  //异常管理器，括号内需要设置异常的类型
    @ResponseBody
    public Object err(Exception e){
        HashMap<String, Object> map = new HashMap<>();
        map.put("status","-1");
        map.put("data","");
        map.put("msg",e.getMessage());
        return map;
    }

    @ExceptionHandler(RuntimeException.class)  //异常管理器，括号内需要设置异常的类型
    @ResponseBody
    public Object err2(RuntimeException e){
        HashMap<String, Object> map = new HashMap<>();
        map.put("status","-1");
        map.put("data","");
        map.put("msg",e.getMessage());
        return map;
    }

    @ExceptionHandler(NullPointerException.class)  //异常管理器，括号内需要设置异常的类型
    @ResponseBody
    public Object err3(NullPointerException e){
        HashMap<String, Object> map = new HashMap<>();
        map.put("status","-1");
        map.put("data","");
        map.put("msg","铁子出现空指针异常了");
        return map;
    }


}
